package com.find.memories.adapter;

import java.util.ArrayList;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;

import com.find.memories.FragmentMap;
import com.find.memories.FragmentMemo;
import com.find.memories.gallery.FragmentGallery;
import com.find.memories.sms.FragmentSMS;

public class TabsAdapter extends FragmentPagerAdapter implements ActionBar.TabListener, ViewPager.OnPageChangeListener {
    private final Context            mContext;
    private final ViewPager          mViewPager;
    private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();

    public static final int      TAB_ORDER_GALLERY        = 0;
    public static final int      TAB_ORDER_SMS               = 1;
    public static final int      TAB_ORDER_MAP              = 2;
    public static final int      TAB_ORDER_MEMO           = 3;
    public static final int      TAB_ORDER_MAX              = 4;
    
    private Fragment mFragmentGallery;
    private Fragment mFragmentSMS;
    private Fragment mFragmentMap;
    private Fragment mFragmentMemo;
    
    static final class TabInfo {
        private final Class<?> clss;
        private final Bundle   args;

        TabInfo(Class<?> _class, Bundle _args) {
            clss = _class;
            args = _args;
        }
    }

    public TabsAdapter(Activity activity, FragmentManager fragmentManager, ViewPager pager) {
        super(fragmentManager);
        mContext = activity;
        mViewPager = pager;
        mViewPager.setAdapter(this);
        mViewPager.setOnPageChangeListener(this);
    }

    @Override
    public int getCount() {
        return TAB_ORDER_MAX;
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case TAB_ORDER_GALLERY:
                if (mFragmentGallery == null)
                        mFragmentGallery = FragmentGallery.init();
                    return mFragmentGallery;
            case TAB_ORDER_SMS:
                if (mFragmentSMS == null)
                        mFragmentSMS = FragmentSMS.init();
                    return mFragmentSMS;
            case TAB_ORDER_MAP:
                if (mFragmentMap == null)
                    mFragmentMap = FragmentMap.init();
                return mFragmentMap;
            case TAB_ORDER_MEMO:
                if (mFragmentMemo == null)
                    mFragmentMemo = FragmentMemo.init();
                return mFragmentMemo;
            default:
                return null;
        }
        
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    }

    @Override
    public void onPageSelected(int position) {
    }

    @Override
    public void onPageScrollStateChanged(int state) {
    }

    @Override
    public void onTabReselected(Tab tab, android.app.FragmentTransaction ft) {

    }

    @Override
    public void onTabSelected(Tab tab, android.app.FragmentTransaction arg1) {
        Object tag = tab.getTag();
        for (int i = 0; i < mTabs.size(); i++) {
            if (mTabs.get(i) == tag) {
                mViewPager.setCurrentItem(i);
            }
        }
    }

    @Override
    public void onTabUnselected(Tab arg0, android.app.FragmentTransaction arg1) {

    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }
    
    
}
